package city.spring.controller;

import city.spring.domain.entity.ClassifyTreeEntity;
import city.spring.service.ClassifyTreeService;
import city.spring.utils.MyBatisUtils;
import city.spring.utils.TreeDataUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.ListMultimap;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：多级分类信息
 *
 * @author 侯坤林
 * @date 2020-04-08 14:20:10
 */
@RestController
@RequestMapping("/classify_tree")
public class ClassifyTreeController extends ApiController {
    private final ClassifyTreeService classifyTreeService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<ClassifyTreeEntity, ?>> entityOrderFields;

    public ClassifyTreeController(ClassifyTreeService classifyTreeService) {
        this.classifyTreeService = classifyTreeService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(ClassifyTreeEntity::getId);
        entityOrderFields.add(ClassifyTreeEntity::getPid);
        entityOrderFields.add(ClassifyTreeEntity::getTitle);
        entityOrderFields.add(ClassifyTreeEntity::getRemarks);
        entityOrderFields.add(ClassifyTreeEntity::getVersion);
        entityOrderFields.add(ClassifyTreeEntity::getGmtCreate);
        entityOrderFields.add(ClassifyTreeEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>多级分类信息</strong> 列表
     */
    @GetMapping("tree")
    public Object listAll() {
        List<ClassifyTreeEntity> list = classifyTreeService.list();
        ListMultimap<String, ClassifyTreeEntity> datasource = TreeDataUtils.handlerTreeDatasource(list, "", ClassifyTreeEntity::getId, ClassifyTreeEntity::getPid, ClassifyTreeEntity::getChildren, ClassifyTreeEntity::setChildren);
        return success(datasource.values());
    }

    /**
     * 获取全部的 <strong>多级分类信息</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, ClassifyTreeEntity entity) {
        LambdaQueryChainWrapper<ClassifyTreeEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>多级分类信息</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, ClassifyTreeEntity entity) {
        LambdaQueryChainWrapper<ClassifyTreeEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<ClassifyTreeEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>多级分类信息</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(classifyTreeService.getById(id));
    }

    /**
     * 添加一个 <strong>多级分类信息</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody ClassifyTreeEntity entity) {
        classifyTreeService.saveClassifyTree(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>多级分类信息</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody ClassifyTreeEntity entity) {
        entity.setId(id);
        classifyTreeService.updateClassifyTree(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 修改一个 <strong>多级分类信息</strong> 的父级和排序，维持与前端表现一致
     *
     * @param entities 信息列表
     */
    @PutMapping("maintain")
    public Object maintainParentAndSorted(@RequestBody List<ClassifyTreeEntity> entities) {
        classifyTreeService.maintainParentAndSorted(entities);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>多级分类信息</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        classifyTreeService.deleteClassifyTree(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>多级分类信息</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        classifyTreeService.deleteClassifyTree(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<ClassifyTreeEntity> buildLambdaQuery(ClassifyTreeEntity entity) {
        LambdaQueryChainWrapper<ClassifyTreeEntity> lambdaQuery = classifyTreeService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), ClassifyTreeEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPid()), ClassifyTreeEntity::getPid, entity.getPid());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTitle()), ClassifyTreeEntity::getTitle, entity.getTitle());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRemarks()), ClassifyTreeEntity::getRemarks, entity.getRemarks());
        lambdaQuery.like(entity.getVersion() != null, ClassifyTreeEntity::getVersion, entity.getVersion());
        lambdaQuery.like(entity.getGmtCreate() != null, ClassifyTreeEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.like(entity.getGmtModified() != null, ClassifyTreeEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

}
